<?php

namespace app\common\model\publics;

use think\Model;


class Indexcate extends Model
{

    

    

	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'publics_indexcate';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
	    'image_url',
        'type_text',
    ];
    

    public static function get_index_cate_category($user_id, $circle_id){
        $recommend_lists = self::get_index_cate_recommend($user_id, $circle_id);
        $class_ids = self::where(['circle_id' => $circle_id])->order("weigh asc")->column('class_id');
        if(!$class_ids){
            return [];
        }
        $field = 'id,name';
        $class_lists = Indexcateclass::field($field)->whereIn('id', $class_ids)->order("weigh asc")->select();
        if(!$class_lists){
            return [];
        }
        $field = 'id,image,type,name,class_id,param as route_url,circle_id';
        foreach ($class_lists as $key => $value){
            $class_lists[$key]['children'] = self::field($field)->where(['class_id' => $value['id'], 'circle_id' => $circle_id])
                ->order("weigh asc")->select();
        }
       //$class_lists = $class_lists->toArray();
        $recommend = ['id' => 0, 'name' => '我的常用','children' => $recommend_lists];
        array_unshift($class_lists, $recommend);
        return $class_lists;
    }

    public static function get_index_cate_recommend($user_id, $circle_id){
        $field = 'id,image,type,name,class_id,param as route_url,circle_id';
        if(!$user_click_ids = Indexcateclick::getUsersClickMany($user_id, $circle_id)){
            return self::field($field)->where(['circle_id' => $circle_id])->order("weigh asc")->limit(0,14)->select();
        }
        /*获取当前圈子的前14个分类*/
        $cate_ids = self::where(['circle_id' => $circle_id])->order("weigh asc")->column('id');
        /*获取用户点击的分类*/
        foreach ($user_click_ids as $key => $value){
            $_index = array_search($value, $cate_ids);
            $_cate_index = (5 + $key);
            if($_index && $_index>= 0) {
                if($_index > 5){
                    list($cate_ids[$_cate_index], $array[$_index]) = [$cate_ids[$_index], $cate_ids[$_cate_index]];
                }
            } else {
                $cate_ids[$_cate_index] = $value;
            }
        }
        if(!$cate_ids = array_values(array_unique($cate_ids))){
            return [];
        }
        return self::field($field)->whereIn('id', $cate_ids)->where(['circle_id' => $circle_id])
            ->order("weigh asc")->limit(0,14)->select();
    }

    /*用户点击金刚区事件*/
    public static function user_click_cate($user_id, $circle_id, $cate_id){
       return Indexcateclick::addCateClick($user_id, $circle_id, $cate_id);
    }


    public static function get_index_cate_content( $cate_id, $circle_id){
        $field = 'id,type,name,content';
        $cate = self::field($field)->where(['id' => $cate_id])->order("weigh asc")->find();
        if(!$cate){
            return [];
        }
        return $cate;
    }

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2'), '3' => __('Type 3')];
    }

    public function getRouteUrlAttr($value, $data)
    {
        if(isset($data['type']) && $data['type'] == 3){
            return 'pagesE/content/content?cate_id='.$data['id'].'&circle_id='. $data['circle_id'];
        }
        if(!isset($data['route_url']) || !$data['route_url']){
            return '';
        }
        if(isset($data['circle_id']) && $data['circle_id']){
            return $value .'?circle_id='. $data['circle_id'];
        }
        return $value;
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

	public function getImageUrlAttr($value, $data){

		if(!$value = $value ?: (isset($data["image"])? $data["image"] : '')){
		    return [];
        }
		return getImagesUrl($value);

	}


	public function circle()
	{
		return $this->belongsTo('app\common\model\Circle', 'circle_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}


}
